<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="begin">开启计时器</button>
    <button class="close">关闭计时器</button>
    <script>
        var begin = document.querySelector(".begin");
        var close = document.querySelector(".close");
        var time = null;
        begin.addEventListener("click", function() {
            time = setInterval(function() {
                console.log(1);
            }, 1000)
        });
        close.addEventListener("click", function() {
            clearInterval(time)
        })
    </script>
</body>

</html>